package com.chixing.util;


import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;

/**
 * Mybatis：sqlSession执行sql语句 ，就是内部封装了一个PreparedStatement
 * PreparedStatement 执行sql语句
 *
 */
public class MybatisUtil {
    private static SqlSessionFactory sqlSessionFactory;
    static {
        InputStream is = MybatisUtil.class.getResourceAsStream("/mybatis-config.xml");
        sqlSessionFactory=new SqlSessionFactoryBuilder().build(is);
    }
    public static SqlSession getSqlSession(){
        SqlSession sqlSession=sqlSessionFactory.openSession();
        return sqlSession;
    }

    public static void closeSqlSession(SqlSession sqlSession){
       if (sqlSession!=null)
           sqlSession.close();
    }

}
